/**
 * Copyright (c) 2011 Roundarch (www.roundarch.com)
 *
 * Author Joshua Jung
 *
 * ---
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.roundarch.flex.controls.graphControlConstraints
{
    import com.roundarch.flex.controls.graphControlInterfaces.IGraphControlSelector;
    import flash.geom.Point;
    import flash.utils.Dictionary;

    public class SelectorConstraintUtil
    {
        /**
         * Applies the provided array of IPointConstraint implementations to the requested
         * point value.
         *
         * Returns null if no resolution could be reached for the requested value.
         */
        public static function applyConstraints(requestedPoint : Point, selector :IGraphControlSelector, constraints : Array) : Point
        {
            // All the points that constraints considered invalid, stored by key Point::toString()
            var invalids : Dictionary=new Dictionary();

            // The new point proposed by the current constraint being monitored.
            var proposed : Point=null;

            // When true a constraint proposed a new point so we have to start our 
            // loop over.
            var redo : Boolean=false;

            /**
             * So this method is c * O(n) because we are only looping on n and c starts at 1,
             * but every time a constraint returns a new value, we start the loop over
             * thus adding 1 to c.
             */
            do
            {
                redo=false;

                for each (var constraint : ISelectorConstraint in constraints)
                {
                    proposed=constraint.constrain(requestedPoint, selector);

                    if (invalids[proposed.toString()])
                    {
                        // We are in deadlock because this constraint or another
                        // constraint already said this proposed value was invalid which
                        // means we will never be able to resolve these constraints.
                        // Return null, meaning the constraints could not be resolved
                        // for the requested point.
                        return null;
                    }

                    if (!pointEquals(proposed, requestedPoint))
                    {
                        // Okay, the requested Point is obviously invalid because the constraint
                        // returned a different proposed point...
                        invalids[requestedPoint.toString()]=true;

                        requestedPoint=proposed;

                        // ... this means we have to reapply all the other constraints.
                        redo=true;
                    }
                }


            } while (redo);

            return proposed;
        }

        public static function pointEquals(p1 : Point, p2 : Point) : Boolean
        {
            return p1.x == p2.x && p1.y == p2.y;
        }
    }
}
